package com.zkingsoft.web.tag;

import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.zkingsoft.web.bean.SearchArticle;
import com.zkingsoft.web.bean.WebArticle;
import com.zkingsoft.web.service.WebArticleService;
import com.zkingsoft.web.service.WebArticleTypeService;
import com.zkingsoft.web.tool.SystemConstance;
import com.zkingsoft.web.tool.Tools;
import com.zkingsoft.web.tool.WebUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchArticleTag extends SimpleTagSupport {

    /**
     * 文章分类的code
     */
    private String code;

    // 分页大小
    private Integer pageSize;
    // 当前分页
    private Integer pageNow;

    /**
     * 变量名称
     */
    private String var;

    /**
     *  搜索字段
     */
    private String query;

    /**
     *  根据字母搜索
     */
    private String letter;

    private final Map<String, String> letters = new HashMap<>();

    {
        letters.put("A", "吖,八");
        letters.put("B", "八,嚓");
        letters.put("C", "嚓,咑");
        letters.put("D", "咑,妸");
        letters.put("E", "妸,发");
        letters.put("F", "发,旮");
        letters.put("G", "旮,铪");
        letters.put("H", "铪,丌");
        letters.put("J", "丌,咔");
        letters.put("K", "咔,垃");
        letters.put("L", "垃,嘸");
        letters.put("M", "嘸,拏");
        letters.put("N", "拏,噢");
        letters.put("O", "噢,妑");
        letters.put("P", "妑,七");
        letters.put("Q", "七,呥");
        letters.put("R", "呥,仨");
        letters.put("S", "仨,他");
        letters.put("T", "他,屲");
        letters.put("W", "屲,夕");
        letters.put("X", "夕,丫");
        letters.put("Y", "丫,帀");
        letters.put("Z", "帀");
    }

    @Override
    public void doTag() throws JspException, IOException {
        WebArticleService artService = WebUtil.getService(this.getJspContext(), SystemConstance.ART_SERVICE);

        SearchArticle search = new SearchArticle();
        search.setCompanyId(Tools.getCompanyId());

        String queryKey;
        if (query.equals(new String(query.getBytes("iso-8859-1"), "iso-8859-1"))) {
            queryKey =  new String(query.getBytes("iso-8859-1"),"utf-8");
        } else {
            queryKey = query;
        }

        search.setQueryKey(queryKey);
        search.setIsPublish(SystemConstance.IS_Y);
        search.setCode(code);

        if (StringUtils.isNotBlank(letter)) {
            String letterGbk = letters.get(letter.toUpperCase());

            if (StringUtils.isNotBlank(letterGbk)) {
                String[] split = letterGbk.split(",");

                search.setLetterPrefix(split[0]);
                if (split.length == 2) {
                    search.setLetterSuffix(split[1]);
                }
                search.setLetter(letter.toUpperCase());
            }
        }

        PaginationVO pageVo = new PaginationVO();
        if (pageSize != null) {
            pageVo.setLimit(pageSize);
        }
        if (pageNow != null) {
            pageVo.setOffset(pageSize * pageNow);
            int recodeCount = artService.findTotalBySearchModel(search);
            int pageCount = 0;
            if (recodeCount % pageSize == 0) {
                pageCount = recodeCount / pageSize;
            } else {
                pageCount = recodeCount / pageSize + 1;
            }
            this.getJspContext().setAttribute("recodeCount", pageCount);
        }

        pageVo.setSort("artCreatetiem");
        pageVo.setOrder("desc");
        List<WebArticle> arts = artService.findBySearchModel(search, pageVo);
        this.getJspContext().setAttribute(var, arts);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNow() {
        return pageNow;
    }

    public void setPageNow(Integer pageNow) {
        this.pageNow = pageNow;
    }

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getLetter() {
        return letter;
    }

    public void setLetter(String letter) {
        this.letter = letter;
    }
}
